package com.service;
import static com.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.def.GenericDef.ACTION_FLG_SEARCH;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IXpinqcodDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.tx.Xpinqcod.XpinqcodI;
import com.tx.Xpinqcod.XpinqcodO;
import com.tx.Xpinqcod.XpinqcodOA;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpinqcodService")
public class XpinqcodService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpinqcodService.class);
	@Autowired
	private IXpinqcodDao inqCodeDao;
	@Autowired
	private XpinqcodI xpinqcodI;
	@Autowired
	private XpinqcodO xpinqcodO;
	@Autowired
	private MqOpe mqOpe;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		Session session = inqCodeDao.getCrSession();
		try{
			xpinqcodI = (XpinqcodI) mqOpe.inStr2Obj(strInMsg,xpinqcodI);
			char action_flg = xpinqcodI.getAction_flg().charAt(0);
			switch(action_flg){
				case ACTION_FLG_INQUIRE : xpinqcodO  = queryCodeProc() ; break;
				case ACTION_FLG_SEARCH  : xpinqcodO  = getCodeByAutrProc() ; break;
				default                 : xpinqcodO = errActionFnc();   
			}
		    if( RETURN_CODE_OK.equals(xpinqcodO.getRtn_code()) ){
		    	session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}		
		}catch(Exception ex){
			xpinqcodO.setTrx_id("XPINQCOD");
			xpinqcodO.setType_id("O");
			xpinqcodO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpinqcodO.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			logger.info(mqOpe.obj2Str(xpinqcodO));
			return mqOpe.obj2Str(xpinqcodO);
		}
	}
	public XpinqcodO queryCodeProc(){
		String sql = " from Bis_data WHERE ";
		String sql_cus = "";
		if( xpinqcodI.getData_cate() !=null){
			sql = sql + " data_cate='"+ xpinqcodI.getData_cate() +"'";
		}
		if( xpinqcodI.getData_id()!=null){
			sql = sql + " AND data_id='"+ xpinqcodI.getData_id() +"'";
		}
		if ( xpinqcodI.getData_ids() != null ) {
			sql = sql + " AND data_id in ("+ xpinqcodI.getData_ids() +")";
		}
		if( xpinqcodI.getOpe_id() != null && !Util.isSpaceCheck(xpinqcodI.getOpe_id())) {
			sql = sql + " AND data_id in (select data_ext from Bis_data where data_cate = 'LNOP' and ext_1 = '"+xpinqcodI.getOpe_id()+"')";
		}
		if( xpinqcodI.getData_ext()!=null){
			sql = sql + " AND data_ext='"+ xpinqcodI.getData_ext() +"'";
		}
		if( xpinqcodI.getData_exts()!=null){
			sql = sql + " AND data_ext in ("+ xpinqcodI.getData_exts() +")";
		}
		if( xpinqcodI.getData_item()!=null){
			sql = sql + " AND data_item='"+ xpinqcodI.getData_item() +"'";
		}
		if( xpinqcodI.getData_items()!=null){
			sql = sql + " AND data_item in ("+ xpinqcodI.getData_items() +")";
		}
		if( xpinqcodI.getExt_1()!=null){
			sql = sql + " AND ext_1='"+ xpinqcodI.getExt_1() +"'";
		}	
		if( xpinqcodI.getExt_1s()!=null){
			sql = sql + " AND ext_1 in ("+ xpinqcodI.getExt_1s() +")";
		}	
		if( xpinqcodI.getExt_2()!=null){
			sql = sql + " AND ext_2='"+ xpinqcodI.getExt_2() +"'";
		}	
		if( xpinqcodI.getExt_2s()!=null){
			sql = sql + " AND ext_2 in ("+ xpinqcodI.getExt_2s() +")";
		}	
		if( xpinqcodI.getExt_3()!=null){
			sql = sql + " AND ext_3='"+ xpinqcodI.getExt_3() +"'";
		}	
		if( xpinqcodI.getExt_3s()!=null){
			sql = sql + " AND ext_3 in ("+ xpinqcodI.getExt_3s() +")";
		}	
		if( xpinqcodI.getExt_4()!=null){
			sql = sql + " AND ext_4='"+ xpinqcodI.getExt_4() +"'";
		}	
		if( xpinqcodI.getExt_4s()!=null){
			sql = sql + " AND ext_4 in ("+ xpinqcodI.getExt_4s() +")";
		}	
		if( xpinqcodI.getExt_5()!=null){
			sql = sql + " AND ext_5='"+ xpinqcodI.getExt_5() +"'";
		}	
		if( xpinqcodI.getExt_5s()!=null){
			sql = sql + " AND ext_5 in ("+ xpinqcodI.getExt_5() +")";
		}	
		logger.info("Query sql:" + sql );
		sql_cus = sql;
		if ( xpinqcodI.getCus_id() != null ){
			sql_cus += " AND data_item='"+ xpinqcodI.getCus_id() +"'";
			sql += " AND (data_item is null or data_item = '')";
		}
		sql_cus = sql_cus + " order by data_id asc";
		sql = sql + " order by data_id asc";
		List<Bis_data> codeList = inqCodeDao.find(sql_cus);
		if (codeList.isEmpty()){
			codeList = inqCodeDao.find(sql);
		}
		XpinqcodO xpinqcodO  = new XpinqcodO();
		List<XpinqcodOA> inqOaryList = new ArrayList<XpinqcodOA>();
	    for(int i=0;i<codeList.size();i++){
	    	XpinqcodOA xpinqcodOA = new XpinqcodOA();
	    	xpinqcodOA.setData_cate(codeList.get(i).getData_cate());
	    	xpinqcodOA.setData_id(codeList.get(i).getData_id());
	    	xpinqcodOA.setData_ext(codeList.get(i).getData_ext());
	    	xpinqcodOA.setData_item(codeList.get(i).getData_item());
	    	xpinqcodOA.setData_desc(codeList.get(i).getData_desc());
	    	xpinqcodOA.setExt_1(codeList.get(i).getExt_1());
	    	xpinqcodOA.setExt_2(codeList.get(i).getExt_2());
	    	xpinqcodOA.setExt_3(codeList.get(i).getExt_3());
	    	xpinqcodOA.setExt_4(codeList.get(i).getExt_4());
	    	xpinqcodOA.setExt_5(codeList.get(i).getExt_5());
	    	xpinqcodOA.setData_seq_id(codeList.get(i).getData_seq_id());
	    	inqOaryList.add(xpinqcodOA);
	    }	
	    xpinqcodO.setTrx_id("XPINQCOD");
	    xpinqcodO.setTrx_id("O");
	    xpinqcodO.setRtn_code(RETURN_CODE_OK);
	    xpinqcodO.setRtn_mesg(RETURN_MESG_OK);
	    xpinqcodO.setTbl_cnt(Integer.toString(inqOaryList.size()));
	    xpinqcodO.setOary(inqOaryList);
		return xpinqcodO ;
	}
	public XpinqcodO getCodeByAutrProc(){
		List<XpinqcodOA> codOaryList = new ArrayList<XpinqcodOA>();
		String sql = " from Bis_data WHERE data_cate='"+ xpinqcodI.getData_cate() +"'order by data_id ";
		List<Bis_data> bis_dataList  = inqCodeDao.find(sql);
		if( bis_dataList.size() > 0){
			for( int i = 0 ;i< bis_dataList.size() ;i++){
				
				Bis_data bis_data = new Bis_data();
				bis_data = bis_dataList.get(i);
				if(  bis_data.getExt_1() != null && bis_data.getExt_1().equalsIgnoreCase("Y")){
					String hsql = "from Bis_data WHERE data_cate='UFGC'AND ext_2 ='" + bis_data.getExt_2()+"'";
					List<Bis_data> UFGCList = inqCodeDao.find(hsql);
					if( UFGCList.size() == 0){
						;
					}else{
						String sqlUser = "from Bis_data WHERE data_cate='UFUG'AND data_ext ='" + xpinqcodI.getData_ext()+"' AND ext_1 = '"+UFGCList.get(0).getExt_1()+"'";
						List<Bis_data> UFUGlist = inqCodeDao.find(sqlUser);
						if( UFUGlist.size() == 0){
							;
						}else{
							codOaryList.add(subSetOutTrx(bis_data));
						}
					}
					
				}else{
					codOaryList.add(subSetOutTrx(bis_data));
				}
			}
		}
		xpinqcodO.setOary(codOaryList);
		xpinqcodO.setTrx_id("XPINQCOD");
	    xpinqcodO.setTrx_id("O");
	    xpinqcodO.setRtn_code(RETURN_CODE_OK);
	    xpinqcodO.setRtn_mesg(RETURN_MESG_OK);
	    xpinqcodO.setTbl_cnt(Integer.toString(codOaryList.size()));
		return xpinqcodO;
	}
	public XpinqcodOA subSetOutTrx( Bis_data bis_data ){
		
		XpinqcodOA  xpinqcodOA = new XpinqcodOA();
		xpinqcodOA.setData_cate(bis_data.getData_cate());
		xpinqcodOA.setData_id(bis_data.getData_id());
		xpinqcodOA.setData_ext(bis_data.getData_ext());
		xpinqcodOA.setData_item(bis_data.getData_item());
		xpinqcodOA.setData_desc(bis_data.getData_desc());
		xpinqcodOA.setExt_1(bis_data.getExt_1());
		xpinqcodOA.setExt_2(bis_data.getExt_2());
		xpinqcodOA.setExt_3(bis_data.getExt_3());
		xpinqcodOA.setExt_4(bis_data.getExt_4());
		xpinqcodOA.setExt_5(bis_data.getExt_5());
		return xpinqcodOA ;
	}	
	public XpinqcodO errActionFnc(){
		XpinqcodO  xplstcodeO = new XpinqcodO();
		xplstcodeO.setTrx_id("XPINQCOD");
		xplstcodeO.setType_id("O");
		xplstcodeO.setRtn_code("0000009");
		xplstcodeO.setRtn_mesg("Invalid Action_flg");
		return xplstcodeO;
	}
	
}
